﻿using System;
using System.Threading;
using EmailMarketingDao;
using EmailMarketingDomain;
using Timer = System.Timers.Timer;

namespace EmailMarketingService
{
    public class EmailSendService
    {
        /// <summary>
        /// 首次开始发信业务
        /// </summary>
        public static void StartToSendEmail()
        {
            Thread th1 = new Thread(NativePostOffice.GetInstance().StartToWork);
            th1.Start();
            Thread th2 = new Thread(QQPostOffice.GetInstance().StartToWork);
            th2.Start();
            Thread th3 = new Thread(One63PostOffice.GetInstance().StartToWork);
            th3.Start();
            Thread th4 = new Thread(One26PostOffice.GetInstance().StartToWork);
            th4.Start();

        }
        /// <summary>
        /// 添加观察邮箱地址到发送队列
        /// </summary>
        /// <param name="email"></param>
        public static void AddEmailAddressToWaiteSend(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
                return;
            Letter letter = new Letter()
            {
                ToSendAddress = email,
                Content = NativePostOffice.LetterTemplate.Content + "(" + DateTime.Now + ")",
                Title = NativePostOffice.LetterTemplate.Title + "(" + DateTime.Now + ")",
            };
            NativePostOffice.AddLetter(letter);
        }
        /// <summary>
        /// 定时更新发件账号和代理邮局每日已发次数，方便自动每日定时访问
        /// </summary>
        public static void AutoUpdateSendAcountHasSendCount()
        {
            Timer t = new Timer();
            t.Interval = 1000 * 60 * 30;//半小时执行一次
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(
            delegate
            {
                var acounts = SendEmailAcountDao.GetAllAcount();
                foreach (var acount in acounts)
                {
                    if (acount.LastLogin != null)
                    {
                        if (DateTime.Now - acount.LastLogin > new TimeSpan(0, 24, 0, 0))
                        {
                            SendEmailAcountDao.UpdateSendAcountHasendCount(acount.Acount,0);
                        }
                    }
                    else
                    {
                        SendEmailAcountDao.UpdateLastLoginTime(acount.Acount,DateTime.Now);
                    }
                }
                var smtps = SmtpServerDao.GetEmailSmtpServcer();
                foreach (var smtpServer in smtps)
                {
                    if (smtpServer.LastResetSendCountTime != null)
                    {
                        if (DateTime.Now - smtpServer.LastResetSendCountTime > new TimeSpan(0, 24, 0, 0))
                        {
                            SmtpServerDao.UpdateSMTPHasendCount(smtpServer.Server, 0);
                            SmtpServerDao.UpdateLastResetSendCountTime(smtpServer.Server,DateTime.Now);
                        }
                    }
                    else
                    {
                        SmtpServerDao.UpdateSMTPHasendCount(smtpServer.Server, 0);
                        SmtpServerDao.UpdateLastResetSendCountTime(smtpServer.Server, DateTime.Now);
                    }
                }
            });

        }
    }
}
